package com.example.administrator.printoutindemo;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class StoreSubmitProject2Adapter extends BaseAdapter {

    private Context mContext = null;
    private List<OutStorageListMaterialInfo> li_info = null;
    private OutStorageListMaterialInfo oli = null;
    private LayoutInflater mInflater = null;

    private List<Map<String, String>> mData;// 存储的EditText值
    private Integer index = -1;

    private class ViewHolder {
        TextView s_sub2_txt_title;
        TextView s_sub2_txt_mid;
        EditText s_sub2_ada_edx_outcount;
    }

    private ViewHolder holder = null;

    public StoreSubmitProject2Adapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<OutStorageListMaterialInfo> li_info) {
        this.li_info = li_info;
        this.mData = new ArrayList<Map<String, String>>();

        if (li_info.size() > 0) {

            for (OutStorageListMaterialInfo ol : li_info) {
//				String title = ol.get_material() + "\n剩余数量：" + ol.get_real_count().trim() + ol.get_unit();
                String title = ol.get_material();
                ;
                String sheng_yu_shu_liang = ol.get_real_count().trim() + ol.get_unit();
                String mid = ol.get_mid() + "";
                String surplus = ol.get_real_count();
                String storename = ol.getStorename();
                String spacename = ol.getSpacename();

                Map<String, String> map = new HashMap<String, String>();

                map.put("list_item_mid", mid);
                map.put("list_item_title", title);
                map.put("list_item_sheng_yu_shu_liang", sheng_yu_shu_liang);
                map.put("list_item_cang_ku_ming_cheng", storename);
                map.put("list_item_cang_wei_ming_cheng", spacename);
                map.put("list_item_surplus", surplus);
                map.put("list_item_inputvalue", "");
                map.put("list_item_materialCode", ol.getMaterialCode());
                map.put("list_item_unit", ol.get_unit());
                mData.add(map);
            }
        }

        this.mInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    }

    public List<Map<String, String>> getListSelected() {
        return mData;
    }


    @Override
    public int getCount() {
        int result = 0;
        if (null != mData) {
            result = mData.size();
        }
        return result;
    }

    @Override
    public Object getItem(int position) {
        Object result = null;
        if (null != mData) {
            result = mData.get(position);
        }

        return result;
    }

    @Override
    public long getItemId(int position) {
        int result = 0;
        if (null != mData) {
            //result = Integer.parseInt(mData.get(position).get("list_item_mid").toString());
            result = position;
        }
        return result;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        convertView = mInflater.inflate(R.layout.store_adapter_sub2_outstorage, null);

        TextView s_sub2_txt_title = (TextView) convertView.findViewById(R.id.s_sub2_txt_title);
        TextView tv_ku_cun_shu_liang = (TextView) convertView.findViewById(R.id.tv_ku_cun_shu_liang);
        TextView tv_cang_ku_ming_cheng = (TextView) convertView.findViewById(R.id.tv_cang_ku_ming_cheng);
        TextView tv_cang_wei_ming_cheng = (TextView) convertView.findViewById(R.id.tv_cang_wei_ming_cheng);
//		TextView s_sub2_txt_mid = (TextView) convertView.findViewById(R.id.s_sub2_txt_mid);
//		TextView s_sub2_txt_surplus = (TextView) convertView.findViewById(R.id.s_sub2_txt_surplus);

        EditText s_sub2_ada_edx_outcount = (EditText) convertView.findViewById(R.id.s_sub2_ada_edx_outcount);

        s_sub2_ada_edx_outcount.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

                mData.get(position).put("list_item_inputvalue", s.toString());// 当EditText数据发生改变的时候存到data变量中
            }
        });

        if (null != this.li_info) {

            String value = mData.get(position).get("list_item_mid");
            if (value != null) {
//				s_sub2_txt_mid.setText(value.toString());
            }
            value = mData.get(position).get("list_item_title");
            if (value != null) {
                s_sub2_txt_title.setText(value.toString());
            }
            //出库数量
            value = mData.get(position).get("list_item_inputvalue");
            if (value != null && !"".equals(value)) {
                s_sub2_ada_edx_outcount.setText(value.toString());
            }
            value = mData.get(position).get("list_item_sheng_yu_shu_liang");
            if (value != null && !"".equals(value)) {
                tv_ku_cun_shu_liang.setText(value.toString());
            }
            //剩余数量
            value = mData.get(position).get("list_item_surplus");
            if (value != null && !"".equals(value)) {
//				s_sub2_txt_surplus.setText(value.toString());
            }
            value = mData.get(position).get("list_item_cang_ku_ming_cheng");
            if (value != null && !"".equals(value)) {
                tv_cang_ku_ming_cheng.setText(value.toString());
            }
            value = mData.get(position).get("list_item_cang_wei_ming_cheng");
            if (value != null && !"".equals(value)) {
                tv_cang_wei_ming_cheng.setText(value.toString());
            }


        }


        return convertView;
    }
}